package com.pd.backend.web.controller;


import com.pd.backend.vo.req.LoginReqVo;
import com.pd.backend.vo.resp.LoginRespVo;
import com.pd.backend.service.PdAuthUserService;
import com.pd.backend.vo.resp.R;
import com.wf.captcha.ArithmeticCaptcha;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * (PdAuthUser)表控制层
 *
 * @author GuoXinChao.126@gmail.com
 * @since 2023-02-27 17:11:37
 */
@RestController
@RequestMapping
public class PdAuthUserController {
    /**
     * 服务对象
     */
    @Resource
    private PdAuthUserService pdAuthUserService;

    @Autowired
    StringRedisTemplate redisTemplate;

    /**
     * 登录接口
     * @param vo
     * @return
     */
    @PostMapping("/api/authority/anno/login")
    public R<LoginRespVo> login(@RequestBody LoginReqVo vo){

        R<LoginRespVo> ret = pdAuthUserService.login(vo);

        if (Objects.isNull(ret)){
            return  R.error("登录失败");
        }
        return ret;
    }

    /**
     * @author: GXC
     * @date: 2022/4/21 11:51
     * @description: 生成验证码
     */

    @GetMapping("/api/authority/anno/captcha")
    public void getCode(HttpServletResponse response, HttpServletRequest request,  String key,Long ts){
        String remoteHost = request.getRemoteHost();
        System.out.println(remoteHost);
        Boolean aBoolean = redisTemplate.hasKey("ip:" + remoteHost);
        if (aBoolean){
            String s = redisTemplate.opsForValue().get("ip:" + remoteHost);
            redisTemplate.opsForValue().set("ip:"+remoteHost,Integer.parseInt(s)+1+"",1, TimeUnit.DAYS);
        }else{
            redisTemplate.opsForValue().set("ip:"+remoteHost,1+"",1, TimeUnit.DAYS);
        }
        String s = redisTemplate.opsForValue().get("ip:" + remoteHost);
        if (Integer.parseInt(s)>=20){
            //生成算数验证码
            ArithmeticCaptcha arithmeticCaptcha = new ArithmeticCaptcha();
            //设置2为算数
            arithmeticCaptcha.setLen(0);
            //验证码结果
            String content = arithmeticCaptcha.text();
            try {

                com.wf.captcha.utils.CaptchaUtil.out(arithmeticCaptcha, request, response);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        System.out.println(key+" "+ts);
        //生成算数验证码
        ArithmeticCaptcha arithmeticCaptcha = new ArithmeticCaptcha();
        //设置2为算数
        arithmeticCaptcha.setLen(2);
        //验证码结果
        String content = arithmeticCaptcha.text();
        //存放到Redis中
        redisTemplate.opsForValue().set("code:"+key, content,5, TimeUnit.MINUTES);

        try {
            com.wf.captcha.utils.CaptchaUtil.out(arithmeticCaptcha, request, response);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

